import {Component, EventEmitter, Input, OnInit, Output} from '@angular/core';
import {SFSchema} from '@delon/form';
import {OuUserSelectAlainSchema} from '../../ou-user/ou-user-select-alain/ou-user-select-alain.component';
import {OuUser} from '../../ou-user/entity/ou-user';
import {OuUserService} from '../../ou-user/ou-user.service';
import {ActivatedRoute} from '@angular/router';

@Component({
    selector: 'ou-user-detail',
    templateUrl: './ou-user-detail.component.html',
    styleUrls: ['./ou-user-detail.component.less']
})
export class OuUserDetailComponent implements OnInit {

    @Output() dataChange = new EventEmitter<any>();

    schema: SFSchema = {
        properties: {
            userName: {
                type: 'string',
                title: '用户名称',
                minLength: 2
            },
            loginName: {
                type: 'string',
                title: '登录名称',
                minLength: 2
            },
            positionId: {
                type: 'string',
                title: '所属岗位',
                ui: {
                    widget: 'ou-position-select-alain'
                } as OuUserSelectAlainSchema,
            },
            tel: {
                type: 'string',
                title: '联系电话',
                format: 'regex',
                pattern: '^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\\d{8})$',
                ui: {
                    errors: {pattern: '请输入有效的电话号码'}
                }
            },
            sex: {
                type: 'string',
                title: '用户性别',
                enum: [{
                    value: 'boy',
                    label: '男'
                }, {
                    value: 'girl',
                    label: '女'
                }],
                ui: {
                    widget: 'radio'
                },
                default: 'boy'
            },
            // officeTel: {
            //     type: 'string',
            //     title: '办公电话',
            //     ui: {
            //         grid: {
            //             span: 15,
            //         },
            //     }
            // },
            // email: {
            //     type: 'string',
            //     title: '电子邮箱',
            //     format: 'email',
            //     ui: {
            //         grid: {
            //             span: 9,
            //         },
            //     }
            // },
            // wechat: {
            //     type: 'string',
            //     title: '微 信 号',
            //     ui: {
            //         grid: {
            //             span: 15,
            //         },
            //     }
            // },
            // qq: {
            //     type: 'string',
            //     title: 'Q Q号码',
            //     ui: {
            //         grid: {
            //             span: 9,
            //         },
            //     }
            // },
            // endDate: {
            //     type: 'string',
            //     title: '失效日期',
            //     format: 'date-time'
            // },
            active: {
                type: 'boolean',
                title: '状态',
                ui: {
                    checkedChildren: '启用',
                    unCheckedChildren: '停用'
                },
                default: true
            },
        },
        required: ['userName', 'loginName']
    } as SFSchema;

    record: OuUser;
    aclEdit = false;

    constructor(
        private ouUserService: OuUserService,
        private activeRoute: ActivatedRoute) {
        this.activeRoute.data.subscribe(
            data => {
                const permissions = data.permissions;
                if (permissions) {
                    this.aclEdit = permissions.indexOf('edit') !== -1;
                }
            }
        );
    }

    _userId: string;

    @Input()
    set userId(_userId: string) {
        this._userId = _userId;
        if (_userId) {
            this.ouUserService.get(_userId).subscribe(res => {
                if (res.success) {
                    this.record = res.message;
                }
            });
        }
    }

    ngOnInit() {
    }

    eventClose(value) {
        if (value) {
            this.dataChange.emit('detail');
        }
    }

}
